﻿    using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class OrderAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "listlatestorders":
                    responseData = GetListLatestOrders();
                    break;

                case "getorder":
                    responseData = GetOrder();
                    break;

                case "listbycustomer":
                    responseData = GetListByCustomerId();
                    break;
                case "search":
                    responseData = Search();
                    break;

                case "listadminuser":
                    responseData = GetListAdminUser();
                    break;

                case "liststore":
                    responseData = GetListStore();
                    break;

                //case "create":
                //    responseData = Create();
                //    break;
                //case "delete":
                //    responseData = Delete();
                //    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData GetListLatestOrders()
        {
            var responseData = new ResponseData();

            try
            {
                var lstOrder = OrderBo.GetByStatus((int)OrderStatus.Temp);

                responseData.Success  = true;
                responseData.Data     = lstOrder;
                responseData.TotalRow = lstOrder.Rows.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetOrder()
        {
            var responseData = new ResponseData();

            var orderId = Utils.GetIdFromRequestString(Utils.OrderId, 0);

            try
            {
                var order = OrderBo.GetById(orderId);
                order.TotalOrderMoney    = Utils.FormatPrice(Utils.ToDecimal(order.TotalValue));
                order.OrderStatus        = Utils.GetOrderStatus(order.Status);
                order.FormatRequiredDate = Utils.FormatIn_HHMM_DDMMYYYY(order.RequiredDate);                

                responseData.Success = true;
                responseData.Data    = order;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetListAdminUser()
        {
            var responseData = new ResponseData();

            var storeId = Utils.GetIdFromRequestString(Utils.StoreId, -2);

            try
            {
                var lstAdminUser = OrderBo.GetListAdminUser(storeId);

                responseData.Success    = true;
                responseData.Data       = lstAdminUser;
                responseData.TotalRow   = lstAdminUser.Rows.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetListStore()
        {
            var responseData = new ResponseData();

            var storeId = Utils.GetIdFromRequestString(Utils.StoreId, -2);

            try
            {
                var lstStore = OrderBo.GetListStore(storeId);

                responseData.Success    = true;
                responseData.Data       = lstStore;
                responseData.TotalRow   = lstStore.Rows.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData GetListByCustomerId()
        {
            var responseData = new ResponseData();
            var customerId = Utils.GetIdFromRequestString(Utils.CustomerId, 0);            

            try
            {
                var lstOrder = OrderBo.GetByCustomerId(customerId);

                responseData.Success    = true;
                responseData.Data       = lstOrder;
                responseData.TotalRow   = lstOrder.Rows.Count;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData Search()
        {
            var responseData = new ResponseData();
            
            var keyword     = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);            
            var status      = Utils.GetIdFromRequestString(Utils.Status, 0);
            var storeId     = Utils.GetIdFromRequestString(Utils.StoreId, 0);
            var adminUserId = Utils.GetIdFromRequestString(Utils.AdminUserId, 0);

            var startDate   = Utils.GetIdFromRequestString(Utils.StartDate, string.Empty);
            var endDate     = Utils.GetIdFromRequestString(Utils.EndDate, string.Empty);                        

            var pageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, Constant.PageIndex);            
            var pageSize    = Utils.GetIdFromRequestString(Utils.PageSize, Constant.PageSize);                        
            
            try
            {
                var totalRecords = 0;
                var lstOrder = OrderBo.Search(keyword, status, storeId, adminUserId, startDate, endDate, pageIndex, pageSize, ref totalRecords);
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    lstOrder[i].ItemIndex           = Utils.CalcItemIndex(i, pageIndex, pageSize);
                    lstOrder[i].FormatOrderDate     = Utils.FormatIn_HHMM_DDMMYYYY(Utils.ToDateTime(lstOrder[i].OrderDate));
                    lstOrder[i].FormatRequiredDate  = Utils.FormatIn_HHMM_DDMMYYYY(Utils.ToDateTime(lstOrder[i].RequiredDate));
                    lstOrder[i].TotalOrderMoney     = Utils.FormatPrice(Utils.ToDecimal(lstOrder[i].TotalValue));
                    lstOrder[i].OrderStatus         = Utils.GetOrderStatus(lstOrder[i].Status);
                }

                responseData.Success    = true;
                responseData.Data       = lstOrder;
                responseData.Pager      = PagerHelper.PageLinks(pageIndex, pageSize, totalRecords, Constant.PageStep, string.Format(UrlHelper.PagerOrder, "{0}"));
                responseData.TotalRow   = totalRecords;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }        
    }
}
